package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.UserShareRecord;
import com.ruoyi.web.enums.CommonDictEnum;
import com.ruoyi.web.service.IUserShareRecordService;
import com.ruoyi.web.vo.UserShareRecordPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户分享记录Controller
 *
 * @author ruoyi
 * @date 2023-01-05
 */
@RestController
@RequestMapping("/share/record")
public class UserShareRecordController extends BaseController {
    @Autowired
    private IUserShareRecordService userShareRecordService;

    /**
     * 上传
     *
     * @param file 文件
     * @return r
     */
    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        return userShareRecordService.upload(file);
    }

    /**
     * 查询用户分享记录列表
     */
    @PreAuthorize("@ss.hasPermi('web:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserShareRecord userShareRecord) {
        startPage();
        List<UserShareRecord> list = userShareRecordService.selectUserShareRecordList(userShareRecord);
        TableDataInfo info = getDataTable(list);
        List<UserShareRecordPageVO> voList = new ArrayList<>();
        for (UserShareRecord user : list) {
            UserShareRecordPageVO vo = new UserShareRecordPageVO();
            BeanUtils.copyProperties(user, vo);
            vo.setIsReward(CommonDictEnum.YesOrNo.findName(vo.getIsReward()));
            voList.add(vo);
        }

        return getDataTable(info, voList);
    }

    /**
     * 导出用户分享记录列表
     */
    @PreAuthorize("@ss.hasPermi('web:record:export')")
    @Log(title = "用户分享记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserShareRecord userShareRecord) {
        List<UserShareRecord> list = userShareRecordService.selectUserShareRecordList(userShareRecord);
        ExcelUtil<UserShareRecord> util = new ExcelUtil<UserShareRecord>(UserShareRecord.class);
        util.exportExcel(response, list, "用户分享记录数据");
    }

    /**
     * 分享记录发放奖励
     */
    @PreAuthorize("@ss.hasPermi('web:record:query')")
    @PutMapping(value = "/{id}")
    public R<String> getInfo(@PathVariable("id") Long id) {
        return userShareRecordService.saveReward(id);
    }

    /**
     * 新增用户分享记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:add')")
    @Log(title = "用户分享记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserShareRecord userShareRecord) {
        return toAjax(userShareRecordService.insertUserShareRecord(userShareRecord));
    }

    /**
     * 修改用户分享记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:edit')")
    @Log(title = "用户分享记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserShareRecord userShareRecord) {
        return toAjax(userShareRecordService.updateUserShareRecord(userShareRecord));
    }

    /**
     * 删除用户分享记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:remove')")
    @Log(title = "用户分享记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userShareRecordService.deleteUserShareRecordByIds(ids));
    }
}
